#include "Index.h"

#ifndef ZERO
	#define ZERO 0
#endif

Index::Index()
	: ID(ZERO),
	current(ZERO)
{
}

Index::Index(const Index & toCopy)
{
	ID = toCopy.ID;
	current = toCopy.current;
	fields = toCopy.fields;
}

Index::~Index()
{
	ID = ZERO;
	current = ZERO;
	fields.clear();
}

void Index::setID(int idIndex)
{	
	ID = idIndex;
}

int Index::getID()
{
	return ID;
}

unsigned int Index::getFieldCount()
{
	return fields.size();
}

void Index::addField(Field * field)
{
//	if(field != NULL)
//	{
		fields.push_back(field->getID());
//	}
}

void Index::removeField(int idField)
{
	unsigned int pos = ZERO;
	bool find = false;
	std::vector<int>::iterator it = fields.begin();
	
	while( it != fields.end() && !find)
	{
		if(idField == (*it))
			find = true;
		else
			pos++;
	}
	if(find)
	{
		it = fields.begin() + pos;
		fields.erase(it);
	}
}

int Index::getIdField(unsigned int pos)
{
	std::vector<int>::iterator it = fields.begin();
	int retorno = ZERO;

	if(pos < fields.size())
	{
		it += pos;
		retorno = (*it);
	}
	return retorno;
}

void Index::start()
{
	current = ZERO;
}

bool Index::hasNext()
{
	return (current < fields.size());
}

int Index::next()
{
	std::vector<int>::iterator it = fields.begin() + current;
	current++;
	return (*it);
}


const Index &Index::operator=(const Index &toAssign)
{
	if(this != &toAssign)
	{
		ID = toAssign.ID;
		current = toAssign.current;
		fields = toAssign.fields;
	}
	return *this;
}


